import sqlite3

def connect_to_database():
    # 连接到数据库或创建数据库
    conn = sqlite3.connect('database.db')
    cursor = conn.cursor()
    return conn, cursor

def close_connection(conn):
    # 关闭数据库连接
    conn.close()

def create_table(cursor):
    # 创建数据表（如果不存在）
    cursor.execute('''CREATE TABLE IF NOT EXISTS all_question (question TEXT)''')

def insert_data(data, cursor, conn):
    # 插入数据
    cursor.execute("INSERT INTO all_question (question) VALUES (?)", (data,))
    conn.commit()

def query_data(cursor):
    # 执行查询
    cursor.execute("SELECT * FROM all_question")

    # 获取查询结果
    results = cursor.fetchall()

    return results

def clear_table(cursor,conn):
    # 清空表数据
    cursor.execute("DELETE FROM all_question")
    # 提交更改
    conn.commit()
    close_connection(conn)

# # 连接到数据库
# conn, cursor = connect_to_database()
#
# # 创建数据表
# create_table(cursor)
#
# # 插入数据
# insert_data("Hello, SQLite!", cursor, conn)
#
# # 查询数据
# results = query_data(cursor)
#
# # 打印查询结果
# for row in results:
#     print(row[0])  # 假设只有一个字段，打印第一个字段的值
#
# # 关闭数据库连接
# close_connection(conn)